#ifndef PROXY_ENCRY_ENCRYSERVER_H_
#define PROXY_ENCRY_ENCRYSERVER_H_

#include "proxy/encry/encrybase.h"
#include "proxy/conn/tcpserver.h"
#include "proxy/ping/pingclient.h"
#include "proxy/curl/curlconn.h"

namespace proxy{

  class EncryServer : public boost::noncopyable,
    public boost::enable_shared_from_this < EncryServer > {
  public:
    typedef boost::shared_ptr<EncryServer> Ptr;
    EncryServer(boost::asio::io_service &accpet_service);
    virtual ~EncryServer();
    bool Start();
    void Stop();
  private:
    bool RealyStart();
    void RunThread(io_service_ptr io_service);
    void OnNewConnect(TcpAccpetServer::Ptr accpet_server,
      socket_ptr connect);
    void OnServerError(TcpAccpetServer::Ptr accpet_server,
      const boost::system::error_code& err);

    // with ping
    void OnPingError(PingClient::Ptr ping_client,
      const boost::system::error_code& err);
    void OnPingSucceed(PingClient::Ptr ping_client);
    void HandleTimeout(const boost::system::error_code& err);

    // With CurlClientSession

    void OnSessionErrorMessage(proxy::CurlClientSession::Ptr curl_session,
      int code);
    void OnLoginSucceed(proxy::CurlClientSession::Ptr curl_session);
    void OnSessionMessage(proxy::CurlClientSession::Ptr curl_session,
      std::string message);
  private:
    TcpAccpetServer::Ptr accpet_server_;
    ios_deque io_services_;
    boost::thread_group thr_grp_;
    std::deque<boost::asio::io_service::work> io_service_work_;
    boost::asio::io_service &accpet_service_;
    bool is_stop_;

    // 
    // PingClient::Ptr ping_client_;
    boost::scoped_ptr<boost::asio::deadline_timer> reconnect_timer_;
    CurlClientSession::Ptr curl_session_;
  };

}

#endif // PROXY_ENCRY_ENCRYSERVER_H_